package com.gupt.youhd.controller;/*
@author name
@data 2023/6/6-16:54
*/

import com.gupt.youhd.common.R;
import com.gupt.youhd.entity.CartItem;
import com.gupt.youhd.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //查询用户当前购物车商品
    @GetMapping
    public R<List<CartItem>> list(HttpServletRequest request,String userName){
        List<CartItem> cartItems = cartService.retrieveAllCartItemByUserId(request,userName);
        return R.success(cartItems);
    }

    //删除用户当前购物车商品
    @DeleteMapping("delete")
    public R<String> delete(HttpServletRequest request,Integer productId){
        cartService.delete(request,productId);
        return R.success("删除成功");
    }

//    添加用户当前购物车商品
    @PostMapping("insert")
    public R<String> insert(HttpServletRequest request,Integer productId){   //  当前添加的id
        cartService.insert(request,productId);
        return R.success("添加成功");    //前端提示用户（弹窗   取消  去购物车
    }

    //修改商品数量 +1
    @PutMapping("update")
    public R<String> update(HttpServletRequest request,Integer productId,int status){
        cartService.update(request,productId,status);
        return R.success("修改成功");
    }


    //批量删除
    @DeleteMapping("batchDel")
    public R<String> batchDel(HttpServletRequest request,@RequestBody List<String> productId){
        cartService.batchDel(request,productId);
        return R.success("删除成功");
    }
}
